#!/usr/bin/python
import sys
import os

Import ('env')
cflags = ''
defines = []
root_build_dir=env['BF_BUILDDIR']

source_files = ['makesdna.c']
header_files = env.Glob('../*.h')

makesdna_tool = env.Clone()
dna = env.Clone()
makesdna_tool.Append(CCFLAGS = '-DBASE_HEADER="\\"source/blender/makesdna/\\"" ')

makesdna_tool.Append (CPPPATH = ['#/intern/guardedalloc',
								 '../../makesdna'])

if env['OURPLATFORM'] == 'linuxcross':
	USE_WINE = True # when cross compiling on linux 64bit this is useful
else:
	USE_WINE = False

if not USE_WINE:
	if env['OURPLATFORM'] == 'linuxcross':
		makesdna_tool.Replace(CC='gcc')
		makesdna_tool.Replace(AR='ar')
		makesdna_tool.Replace(LINK='gcc')

if sys.platform != 'cygwin':
	makesdna_tool.Append (CCFLAGS = cflags)
makesdna_tool.Append (CPPDEFINES = defines)
targetdir = root_build_dir+'/lib'
if not (root_build_dir[0]==os.sep or root_build_dir[1]==':'):
	targetdir = '#'+targetdir
makesdna_tool.Append (LIBPATH = targetdir)
if env['BF_PROFILE']:
	makesdna_tool.Append (LINKFLAGS = env['BF_PROFILE_LINKFLAGS'])

targetdir = root_build_dir + '/makesdna'

if not (root_build_dir[0]==os.sep or root_build_dir[1]==':'):
	targetdir = '#' + targetdir

makesdna = makesdna_tool.Program (target = targetdir, source = source_files, LIBS=['bf_guardedalloc'])

dna_dict = dna.Dictionary()
dna.Depends ('dna.c', makesdna)
dna.Depends ('dna.c', header_files)
if env['OURPLATFORM'] != 'linuxcross':
	if USE_WINE:
		dna.Command ('dna.c', '', 'wine ' + root_build_dir+os.sep+"makesdna $TARGET")
	else:
		dna.Command ('dna.c', '', root_build_dir+os.sep+"makesdna $TARGET")
else:
	dna.Command ('dna.c', '', root_build_dir+os.sep+"makesdna.exe $TARGET")
obj = 'intern/dna.c'
Return ('obj')
